package com.zjmzxfzhl.modules.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.purchase.controller.dto.PurchasePaymentQueryDto;
import com.zjmzxfzhl.modules.purchase.entity.PurchasePayment;
import com.zjmzxfzhl.modules.purchase.entity.PurchasePaymentModel;
import com.zjmzxfzhl.modules.purchase.mapper.PurchasePaymentMapper;
import com.zjmzxfzhl.modules.purchase.service.PurchasePaymentService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Service
 *
 * @author 庄金明
 */
@Service
public class PurchasePaymentServiceImpl extends BaseServiceImpl<PurchasePaymentMapper, PurchasePayment>
        implements PurchasePaymentService {
    @Override
    public IPage<PurchasePayment> list(IPage<PurchasePayment> page, PurchasePayment purchasePayment) {
        return page.setRecords(baseMapper.list(page, purchasePayment));
    }

    @Override
    public void deleteByContractId(Long contractId) {
        PurchasePayment payment = new PurchasePayment();
        payment.setContractId(contractId);
        this.baseMapper.delete(new QueryWrapper<>(payment));
    }

    @Override
    public String mergePaymentSituation(Long contractId) {
        List<PurchasePayment> payments = this.listByContractId(contractId);
        return mergePaymentSituation(payments);
    }

    @Override
    public String mergePaymentSituation(List<PurchasePayment> purchasePayments) {
        StringBuilder sb = new StringBuilder("");
        if (!CollectionUtils.isEmpty(purchasePayments)) {
            for (PurchasePayment payment : purchasePayments) {
                sb.append(payment + "\n");
            }
        }
        return sb.toString();
    }

    @Override
    public List<PurchasePayment> listByContractId(Long contractId) {
        List<PurchasePayment> payments = this.list(new QueryWrapper<>(new PurchasePayment(contractId)));
        return payments;
    }

    @Override
    public List<PurchasePaymentModel> listBy(PurchasePaymentQueryDto queryDto) {
        List<PurchasePaymentModel> payments = this.baseMapper.listBy(queryDto);
        return payments;
    }
}
